#include "Manage.h"
#if 0
9341  stainfo 格式
c0:f2:fb:8c:c0:48^^^^1^^^^5^^^^65M^^^^24M^46(48,29,0)^^HT20^^^^^^78^^^^0^^^^79^^2368^ESs^^^^^^^^^^^0^^^^1f^Q^^^^^^^^WME
c8:33:4b:75:7d:07^^^^2^^^^5^^^^^0M^^^^65M^38(36,31,0)^^HT20^^^^^^65^^^15^^^^26^^^672^ESs^^^^^^^^^^^0^^^^1f^Q^^^^^^^^WME
30:92:f6:50:62:25^^^^3^^^^5^^^150M^^^^^1M^56(58,56,0)^^HT40-^^^^^35^^^^0^^^^^8^^^320^ESs^^^^^^^^^^^0^^^^1f^WQGS^^^^^WME
*****************-----*****------+******+---+++++++++*******-------*****------******----************------********-----
#endif
/*****************************
 **函数功能：清除sta数据库的信息
 **参数：mysql:mysql连接表示套接字；APmac:本机mac地址
 **返回值：SUCCESS，fAILED
 *****************************/
int fomat_dataBS(MYSQL mysql,char *APmac)
{  
	char query[64];  
	int flag;  
	sprintf(query, "delete from %s where APmac=\"%s\"", DATABS_STAINFO_TAB, APmac);
	flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));  
	if(flag)
	{  
		printf("Delete data failure!\n");  
		return fAILED;  
	}  
	return SUCCESS; 
}

/*****************************
 **函数功能：修改在在线状态信息位
 **参数：mysql:mysql连接表示套接字；APmac:本机mac地址;stacount:连接本机无线的station数量
 **返回值：SUCCESS，fAILED
 *****************************/
int Change_OL_Status(MYSQL mysql, char *APmac)
{
	char putvalue[256] = { '\0' };
	sprintf(putvalue, "UPDATE %s SET APstate = '1',STA_count = '%d' where APmac = '%s'", DATABS_APMANAGE_TAB, gsta_count, APmac);
	int flag = mysql_real_query(&mysql, putvalue, (unsigned int)strlen(putvalue));
	if (flag)
	{
		printf("Insert data failure!\n");
		return fAILED;
	}
	else
	{
		printf("Insert data success!\n");
	}
	return SUCCESS;
}

/***********************
 **函数功能：更新数据库
 **参数：mysql:mysql连接表示套接字；head:获得的sta信息的链表;stacount:本地连接的sta数量
 **返回值：SUCCESS，fAILED
 ************************/
int uppload_dataBS(MYSQL mysql, STAINFO *head, int *stacount)
{
	if (!head)
	{
		return fAILED;
	}
	STAINFO *p = NULL;   
	char putvalue[256];
	int flag, t;  
	for(p = head;p!= NULL;p = p->next)
	{
		*stacount++;
		memset(putvalue, '\0', 256);
		sprintf(putvalue, "INSERT INTO %s VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')",
			DATABS_STAINFO_TAB, gAP_macaddr, p->mac, p->aid, p->psm, p->mimops, p->mcs, p->bw, p->sgi, p->stbc);
		printf("mysql    %s\n", putvalue);
		flag = mysql_real_query(&mysql, putvalue, (unsigned int)strlen(putvalue));  
		if(flag) 
		{  
			printf("Insert data failure!\n");  
			return fAILED;  
		}
		printf("Insert data success!\n");  
	}
	return SUCCESS; 
}
#if 0
/*****************************
**函数功能：获取AP上网口流量
**参数：apmacaddr:本机mac地址
**返回值：STAINFO *类型的链表头指针
*****************************/
int get_AP_flow(char *rxbyt, char *txbyt)
{
	FILE * rfd = NULL;
	rfd = popen(GET_RX_BYT,"r");
	if (NULL == rfd)
	{
		return fAILED;
	}
	fgets(rxbyt, 24, rfd);
	pclose(rfd);
	rfd = NULL;
	rfd = popen(GET_TX_BYT,"r");
	{
		return fAILED;
	}
	fgets(rxbyt, 24, rfd);
	pclose(rfd);
	rfd = NULL;
	return SUCCESS;
}

#endif

/*****************************
 **函数功能：获取AP上sta的信息
 **返回值：STAINFO *类型的链表头指针
 *****************************/
STAINFO *get_station_info()
{
	printf("get station information... ...\n");
	STAINFO *head =NULL;
	STAINFO *p = NULL;
	FILE *rfd = NULL;
	int i = 0, offer = 0;
	char tmpbuf[32];
	//rfd = popen("get_stainfo.sh", "r");
#ifdef WTP_9341
	system("get_stainfo.sh 9341");
#endif
#ifdef WTP_9557
	system("get_stainfo.sh 9557");
#endif
#ifdef WTP_9557_DEF
	system("get_stainfo.sh 9557_DEF");
#endif
#ifdef WTP_7620
	system("get_stainfo.sh 7620");
#endif
	rfd = fopen("/tmp/stainfo", "r");
	if (NULL == rfd)
	{
		printf("open file fault\n");
		gsta_count = 0;
		return NULL;
	}
	while (!feof(rfd))
	{
		p = (STAINFO *)malloc(sizeof(STAINFO));
		if (NULL == p)
		{
			printf("malloc error!\n");
			break;
		}
		//if (fscanf(rfd, "%s\t%d\t%d\t%s\t%d\t%d\t%d\n", p->mac, &(p->aid), &(p->mimops), p->mcs, &(p->bw), &(p->sgi), &(p->stbc)) <= 0)
		if (fscanf(rfd, "%s\t%s\t%s\t%s\t%s\t%s", p->mac, p->aid, p->mimops, p->mcs, p->bw, p->sgi) <= 0)
			break;
		strcpy(p->psm, "24");
		strcpy(p->stbc, "15");
		printf("scanf valus is\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", p->mac, p->aid, p->mimops, p->mcs, p->bw, p->sgi, p->stbc);
		p->next = head;
		head = p;
		p = NULL;
		i++;
	}
	gsta_count = i;
	fclose(rfd);
	rfd = NULL;
	printf("\n\n");
	return head;
}

/*******************************
 **函数功能：释放无用链表
 **参数：head:需要释放的链表的头指针
 **返回值：无
 *****************************/
void free_list(STAINFO *head)
{
	if (NULL == head)
		return;
	STAINFO *p = NULL, *pp = NULL;
	while (head->next != NULL)
	{
		p = head;
		while (1)
		{
			if (NULL == p->next)
			{
				free(p);
				p = NULL;
				pp->next = NULL;
				break;
			}
			pp = p;
			p = p->next;
		}
	}
	free(head);
	head = NULL;
	return;
}


int Update_Station_Info(char * AC_DB_Ipaddr)
{
	MYSQL mysql;
	STAINFO *p = NULL;
	STAINFO *head = NULL;
	char rxbyt[20] = {'\0'};
	char txbyt[20] = {'\0'};
	int i, stacount;
	if (NULL == AC_DB_Ipaddr)
	{
		AC_DB_Ipaddr = DEFAULT_ACIP;
	}
	while (1)
	{
		sleep(20);
		if (head)
		{
			free_list(head);
		}
		head = get_station_info();//获得在线用户信息
		if (NULL == head)
		{
			printf("no sta information \n");
		}
		else
			printf("get_station_info ok\n");
		mysql_init(&mysql);
		for (i = 0; i < 2; i++)
		{
			if (SUCCESS == connect_dataBS(&mysql, AC_DB_Ipaddr))
			{
				printf("connect dataBS SUCCESS\n");
				break;
			}
			sleep(6);
		}
		if (i >= 2)
		{
			return fAILED;
		}
		for (i = 0; i < 3; i++)
		{
			if (SUCCESS == Change_OL_Status(mysql, gAP_macaddr))//修改AP的在线状态
			{
				printf("Change AP_status SUCCESS\n");
				break;
			}
		}
		fomat_dataBS(mysql, gAP_macaddr); //清空在线用户数据库

		if (NULL != head)
		{
			for (i = 0; i < 3; i++)
			{
				if (SUCCESS == uppload_dataBS(mysql, head, &stacount))//更新数据库
				{
					printf("update stainfo SUCCESS\n");
					break;
				}
				sleep(1);
			}
		}
		else
		{
			printf("no sta information \n");
		}
		mysql_close(&mysql);
		if (head)
		{
			free_list(head);
		}
		sleep(25);
		break;
	}
	return SUCCESS;
}
#if 1
int main(int Argc, char * Argv[])
{
	memset(gAP_macaddr, '\0', sizeof(gAP_macaddr));
	memset(gAP_IPaddr, '\0', sizeof(gAP_IPaddr));
	memset(gAC_IPaddr, '\0', sizeof(gAC_IPaddr));
	int count = 0;
	for (count = 0; count < 2; count++)
	{
		if (SUCCESS == Get_AP_MacAddr(gAP_macaddr))
			break;
	}
	if (count >= 2)
		return fAILED;
	for (count = 0; count < 2; count++)
	{
		if (SUCCESS == Get_ipaddr(gAP_IPaddr))
			break;
	}
	if (count >= 2)
		return fAILED;
	for (count = 0; count < 2; count++)
	{
		if (SUCCESS == Get_AC_IP(gAC_IPaddr))
			break;
	}
	if (count >= 2)
		return fAILED;
	printf("ACIP : %s\nAPIP : %s\nAPMAC : %s\n", gAC_IPaddr, gAP_IPaddr, gAP_macaddr);
	FILE *fd = NULL;
	while (1)
	{
		sleep(15);
		fd = fopen("/tmp/quit_WTP", "w");
		if (fd)
		{
			fclose(fd);
			fd = NULL;
		}
		Update_Station_Info(gAC_IPaddr);
		
	}
	return SUCCESS;
	
}

#endif
